package com.zcy.view;

import com.zcy.model.Brand;
import com.zcy.model.Cell;
import com.zcy.model.Layer;
import com.zcy.model.Map;
import com.zcy.utils.MapUtils;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class Start extends JFrame {
    public static void main(String[] args) {
        new Start();
    }
    private Brand bj = new Brand("背景草地");
    private Brand xc = new Brand("消除区域");
    public static Map m = MapUtils.builerMap();
    public Start(){
        init();
        initMap();
        xc.setBounds(0,575,450,800);
        this.getContentPane().add(xc);
        bj.setBounds(0,0,450,800);
        this.getContentPane().add(bj);
        autoFlush();
    }
    public void initMap(){
        List<Layer> list = m.getList();
        for (int i = 0;i < list.size();i++){
            Layer layer = list.get(i);
            renderLayer(this.getContentPane(),layer);
        }
    }
    public void renderLayer(Container container, Layer layer){
        Cell[][] cells = layer.getCells();
        for(int row = 0;row < cells.length;row++){
            for (int col = 0;col < cells[row].length;col++){
                Cell cell = cells[row][col];
                if (cell.getState() == 1){
                    Brand b = cell.getBrand();
                    int x = col * 50 + layer.getOffsetx();
                    int y = row * 50 + layer.getOffsety();
                    b.setBounds(x,y,50,50);
                    this.getContentPane().add(b);
                }
            }
        }
    }
    public void autoFlush(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        repaint();
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();
    }
    public void init(){
        this.setVisible(true);
        this.setSize(450,800);
        this.setTitle("羊了个羊");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setBounds(0,0,450,800);
        this.setLocationRelativeTo(null);
    }
}
